package com.example.demo.base;

import com.baomidou.mybatisplus.plugins.Page;

public class Pageable<T> extends Page<T> {

    public static final int MAX_PAGE_SIZE=10;

    public Pageable(){

    }

    public Pageable(int current, int size) {
        super(current, size);
    }

    public Pageable(int current, int size, String orderByField) {
        super(current, size);
        this.setOrderByField(orderByField);
    }

    public Pageable<T> convert(Page<T> page){
        this.setTotal(page.getTotal());
        this.setSize(page.getSize());
        this.setCurrent(page.getCurrent());
        this.setSearchCount(page.isSearchCount());
        this.setRecords(page.getRecords());
        return this;
    }

    @Override
    public long getPages() {
        long pages=0;
        if (this.getSize() == 0) {
            return 0;
        } else {
            pages = this.getTotal() / this.getSize();
            if (this.getTotal() % this.getSize() != 0) {
                ++pages;
            }

            return pages;
        }
    }

    @Override
    public boolean hasNext() {
        return this.getCurrent() < this.getPages();
    }
    
    public int getStart(){
    	return (getCurrent()-1)*getSize();
    }
}
